# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    """
    根据二叉搜索树的特性，进行递归查找：当前节点比p和q大，则公共祖先一定在左子树；
                                 当前节点比p和q小，则公共祖先一定在右子树；
                                 不满足上述两个条件，则当前节点为公共子祖先；
    """
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        if p.val < root.val and q.val < root.val:
            return self.lowestCommonAncestor(root.left, p, q)
        if p.val > root.val and q.val > root.val:
            return self.lowestCommonAncestor(root.right, p, q)
        return root
